<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>browserchannel.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_browserchannel.js.html">browserchannel.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of the BrowserChannel class.  A BrowserChannel
<a name="line17"></a> * simulates a bidirectional socket over HTTP. It is the basis of the
<a name="line18"></a> * Gmail Chat IM connections to the server.
<a name="line19"></a> *
<a name="line20"></a> * See http://wiki/Main/BrowserChannel
<a name="line21"></a> * This doesn&#39;t yet completely comform to the design document as we&#39;ve done
<a name="line22"></a> * some renaming and cleanup in the design document that hasn&#39;t yet been
<a name="line23"></a> * implemented in the protocol.
<a name="line24"></a> *
<a name="line25"></a> * Typical usage will look like
<a name="line26"></a> *  var handler = [handler object];
<a name="line27"></a> *  var channel = new BrowserChannel(clientVersion);
<a name="line28"></a> *  channel.setHandler(handler);
<a name="line29"></a> *  channel.connect(&#39;channel/test&#39;, &#39;channel/bind&#39;);
<a name="line30"></a> *
<a name="line31"></a> * See goog.net.BrowserChannel.Handler for the handler interface.
<a name="line32"></a> *
<a name="line33"></a> *
<a name="line34"></a> */
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>goog.provide(&#39;goog.net.BrowserChannel&#39;);
<a name="line38"></a>goog.provide(&#39;goog.net.BrowserChannel.Handler&#39;);
<a name="line39"></a>goog.provide(&#39;goog.net.BrowserChannel.LogSaver&#39;);
<a name="line40"></a>goog.provide(&#39;goog.net.BrowserChannel.StatEvent&#39;);
<a name="line41"></a>
<a name="line42"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line43"></a>goog.require(&#39;goog.debug.TextFormatter&#39;);
<a name="line44"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line45"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line46"></a>goog.require(&#39;goog.net.BrowserTestChannel&#39;);
<a name="line47"></a>goog.require(&#39;goog.net.ChannelDebug&#39;);
<a name="line48"></a>goog.require(&#39;goog.net.ChannelRequest&#39;);
<a name="line49"></a>goog.require(&#39;goog.string&#39;);
<a name="line50"></a>goog.require(&#39;goog.structs.CircularBuffer&#39;);
<a name="line51"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * Encapsulates the logic for a single BrowserChannel.
<a name="line56"></a> *
<a name="line57"></a> * @param {string} clientVersion An application-specific version number that
<a name="line58"></a> *        is sent to the server when connected.
<a name="line59"></a> * @constructor
<a name="line60"></a> */
<a name="line61"></a>goog.net.BrowserChannel = function(clientVersion) {
<a name="line62"></a>  /**
<a name="line63"></a>   * The application specific version that is passed to the server.
<a name="line64"></a>   * @type {string}
<a name="line65"></a>   * @private
<a name="line66"></a>   */
<a name="line67"></a>  this.clientVersion_ = clientVersion;
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * The current state of the BrowserChannel. It should be one of the
<a name="line71"></a>   * goog.net.BrowserChannel.State constants.
<a name="line72"></a>   * @type {goog.net.BrowserChannel.State}
<a name="line73"></a>   * @private
<a name="line74"></a>   */
<a name="line75"></a>  this.state_ = goog.net.BrowserChannel.State.INIT;
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * An array of queued maps that need to be sent to the server.
<a name="line79"></a>   * @type {Array.&lt;Object|goog.structs.Map&gt;}
<a name="line80"></a>   * @private
<a name="line81"></a>   */
<a name="line82"></a>  this.outgoingMaps_ = [];
<a name="line83"></a>
<a name="line84"></a>  /**
<a name="line85"></a>   * The channel debug used for browserchannel logging
<a name="line86"></a>   * @type {goog.net.ChannelDebug}
<a name="line87"></a>   * @private
<a name="line88"></a>   */
<a name="line89"></a>  this.channelDebug_ = new goog.net.ChannelDebug();
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Extra HTTP headers to add to all the requests sent to the server.
<a name="line95"></a> * @type {Object}
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.net.BrowserChannel.prototype.extraHeaders_ = null;
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Extra parameters to add to all the requests sent to the server.
<a name="line102"></a> * @type {Object?}
<a name="line103"></a> * @private
<a name="line104"></a> */
<a name="line105"></a>goog.net.BrowserChannel.prototype.extraParams_ = null;
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * The current ChannelRequest object for the forwardchannel.
<a name="line109"></a> * @type {goog.net.ChannelRequest?}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.net.BrowserChannel.prototype.forwardChannelRequest_ = null;
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * The ChannelRequest object for the backchannel.
<a name="line116"></a> * @type {goog.net.ChannelRequest?}
<a name="line117"></a> * @private
<a name="line118"></a> */
<a name="line119"></a>goog.net.BrowserChannel.prototype.backChannelRequest_ = null;
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * The relative path (in the context of the the page hosting the browser
<a name="line123"></a> * channel) for making requests to the server.
<a name="line124"></a> * @type {string?}
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.net.BrowserChannel.prototype.path_ = null;
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * The absolute URI for the forwardchannel request.
<a name="line131"></a> * @type {goog.Uri}
<a name="line132"></a> * @private
<a name="line133"></a> */
<a name="line134"></a>goog.net.BrowserChannel.prototype.forwardChannelUri_ = null;
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * The absolute URI for the backchannel request.
<a name="line138"></a> * @type {goog.Uri}
<a name="line139"></a> * @private
<a name="line140"></a> */
<a name="line141"></a>goog.net.BrowserChannel.prototype.backChannelUri_ = null;
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * A subdomain prefix for using a subdomain in IE for the backchannel
<a name="line145"></a> * requests.
<a name="line146"></a> * @type {string?}
<a name="line147"></a> * @private
<a name="line148"></a> */
<a name="line149"></a>goog.net.BrowserChannel.prototype.hostPrefix_ = null;
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Whether we allow the use of a subdomain in IE for the backchannel requests.
<a name="line153"></a> * @private
<a name="line154"></a> */
<a name="line155"></a>goog.net.BrowserChannel.prototype.allowHostPrefix_ = true;
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * The next id to use for the RID (request identifier) parameter. This
<a name="line159"></a> * identifier uniquely identifies the forward channel request.
<a name="line160"></a> * @type {number}
<a name="line161"></a> * @private
<a name="line162"></a> */
<a name="line163"></a>goog.net.BrowserChannel.prototype.nextRid_ = 0;
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Whether to fail forward-channel requests after one try, or after a few tries.
<a name="line167"></a> * @type {boolean}
<a name="line168"></a> * @private
<a name="line169"></a> */
<a name="line170"></a>goog.net.BrowserChannel.prototype.failFast_ = false;
<a name="line171"></a>
<a name="line172"></a>/**
<a name="line173"></a> * The handler that receive callbacks for state changes and data.
<a name="line174"></a> * @type {goog.net.BrowserChannel.Handler}
<a name="line175"></a> * @private
<a name="line176"></a> */
<a name="line177"></a>goog.net.BrowserChannel.prototype.handler_ = null;
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Timer identifier for asynchronously making a forward channel request.
<a name="line181"></a> * @type {number?}
<a name="line182"></a> * @private
<a name="line183"></a> */
<a name="line184"></a>goog.net.BrowserChannel.prototype.forwardChannelTimerId_ = null;
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Timer identifier for asynchronously making a back channel request.
<a name="line188"></a> * @type {number?}
<a name="line189"></a> * @private
<a name="line190"></a> */
<a name="line191"></a>goog.net.BrowserChannel.prototype.backChannelTimerId_ = null;
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * The BrowserTestChannel object which encapsulates the logic for determining
<a name="line195"></a> * interesting network conditions about the client.
<a name="line196"></a> * @type {goog.net.BrowserTestChannel?}
<a name="line197"></a> * @private
<a name="line198"></a> */
<a name="line199"></a>goog.net.BrowserChannel.prototype.connectionTest_ = null;
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Whether the client&#39;s network conditions can support chunked responses.
<a name="line203"></a> * @type {boolean?}
<a name="line204"></a> * @private
<a name="line205"></a> */
<a name="line206"></a>goog.net.BrowserChannel.prototype.useChunked_ = null;
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Whether chunked mode is allowed. In certain debugging situations, it&#39;s
<a name="line210"></a> * useful to disable this.
<a name="line211"></a> * @private
<a name="line212"></a> */
<a name="line213"></a>goog.net.BrowserChannel.prototype.allowChunkedMode_ = true;
<a name="line214"></a>
<a name="line215"></a>/**
<a name="line216"></a> * The array identifier of the last array received from the server for the
<a name="line217"></a> * backchannel request.
<a name="line218"></a> * @type {number}
<a name="line219"></a> * @private
<a name="line220"></a> */
<a name="line221"></a>goog.net.BrowserChannel.prototype.lastArrayId_ = -1;
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * The last status code received.
<a name="line225"></a> * @type {number}
<a name="line226"></a> * @private
<a name="line227"></a> */
<a name="line228"></a>goog.net.BrowserChannel.prototype.lastStatusCode_ = -1;
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Enum type for the browser channel state machine.
<a name="line233"></a> * @enum {number}
<a name="line234"></a> */
<a name="line235"></a>goog.net.BrowserChannel.State = {
<a name="line236"></a>  /** The channel is closed. */
<a name="line237"></a>  CLOSED: 0,
<a name="line238"></a>
<a name="line239"></a>  /** The channel has been initialized but hasn&#39;t yet initiated a connection. */
<a name="line240"></a>  INIT: 1,
<a name="line241"></a>
<a name="line242"></a>  /** The channel is in the process of opening a connection to the server. */
<a name="line243"></a>  OPENING: 2,
<a name="line244"></a>
<a name="line245"></a>  /** The channel is open. */
<a name="line246"></a>  OPENED: 3
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Maximum number of attempts to connect to the server for forward channel
<a name="line252"></a> * requests.
<a name="line253"></a> * @type {number}
<a name="line254"></a> */
<a name="line255"></a>goog.net.BrowserChannel.FORWARD_CHANNEL_MAX_RETRIES = 2;
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * The timeout in milliseconds for a forward channel request.
<a name="line259"></a> * @type {number}
<a name="line260"></a> */
<a name="line261"></a>goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT = 20 * 1000;
<a name="line262"></a>
<a name="line263"></a>/**
<a name="line264"></a> * Maximum number of attempts to connect to the server for back channel
<a name="line265"></a> * requests that are performed over XMLHTTP.
<a name="line266"></a> * @type {number}
<a name="line267"></a> */
<a name="line268"></a>goog.net.BrowserChannel.BACK_CHANNEL_XMHTTP_MAX_RETRIES = 3;
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/**
<a name="line272"></a> * The version of the protocol used.
<a name="line273"></a> * @type {number}
<a name="line274"></a> */
<a name="line275"></a>goog.net.BrowserChannel.VERSION = 6;
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Enum type for identifying a BrowserChannel error.
<a name="line280"></a> * @enum {number}
<a name="line281"></a> */
<a name="line282"></a>goog.net.BrowserChannel.Error = {
<a name="line283"></a>  /** Value that indicates no error has occurred. */
<a name="line284"></a>  OK: 0,
<a name="line285"></a>
<a name="line286"></a>  /** An error due to a request failing. */
<a name="line287"></a>  REQUEST_FAILED: 2,
<a name="line288"></a>
<a name="line289"></a>  /** An error due to the user being logged out. */
<a name="line290"></a>  LOGGED_OUT: 4,
<a name="line291"></a>
<a name="line292"></a>  /** An error due to server response which contains no data. */
<a name="line293"></a>  NO_DATA: 5,
<a name="line294"></a>
<a name="line295"></a>  /** An error due to a server response indicating an unknown session id */
<a name="line296"></a>  UNKNOWN_SESSION_ID: 6,
<a name="line297"></a>
<a name="line298"></a>  /** An error due to a server response requesting to stop the channel. */
<a name="line299"></a>  STOP: 7,
<a name="line300"></a>
<a name="line301"></a>  /** A general network error. */
<a name="line302"></a>  NETWORK: 8,
<a name="line303"></a>
<a name="line304"></a>  /** An error due to the channel being blocked by a network administrator. */
<a name="line305"></a>  BLOCKED: 9,
<a name="line306"></a>
<a name="line307"></a>  /** An error due to bad data being returned from the server. */
<a name="line308"></a>  BAD_DATA: 10,
<a name="line309"></a>
<a name="line310"></a>  /** An error due to a response that doesn&#39;t start with the magic cookie. */
<a name="line311"></a>  BAD_RESPONSE: 11
<a name="line312"></a>};
<a name="line313"></a>
<a name="line314"></a>
<a name="line315"></a>/**
<a name="line316"></a> * Singleton event target for firing stat events
<a name="line317"></a> * @type {goog.events.EventTarget}
<a name="line318"></a> * @private
<a name="line319"></a> */
<a name="line320"></a>goog.net.BrowserChannel.statEventTarget_ = new goog.events.EventTarget();
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * Events fired by BrowserChannel and associated objects
<a name="line324"></a> * @type {Object}
<a name="line325"></a> */
<a name="line326"></a>goog.net.BrowserChannel.Event = {};
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>/**
<a name="line330"></a> * Stat Event which fires when things of interest happen that mey be useful for
<a name="line331"></a> * applications to know about for stats or debugging purposes. This event fires
<a name="line332"></a> * on the EventTarget returned by getStatEventTarget.
<a name="line333"></a> */
<a name="line334"></a>goog.net.BrowserChannel.Event.STAT_EVENT = &#39;statevent&#39;;
<a name="line335"></a>
<a name="line336"></a>
<a name="line337"></a>/**
<a name="line338"></a> * Event class for goog.net.BrowserChannel.Event.STAT_EVENT
<a name="line339"></a> *
<a name="line340"></a> * @param {goog.events.EventTarget} eventTarget  The stat event target for
<a name="line341"></a>       the browser channel.
<a name="line342"></a> * @param {goog.net.BrowserChannel.Stat} stat The stat.
<a name="line343"></a> * @constructor
<a name="line344"></a> * @extends {goog.events.Event}
<a name="line345"></a> */
<a name="line346"></a>goog.net.BrowserChannel.StatEvent = function(eventTarget, stat) {
<a name="line347"></a>  goog.events.Event.call(this, goog.net.BrowserChannel.Event.STAT_EVENT,
<a name="line348"></a>      eventTarget);
<a name="line349"></a>
<a name="line350"></a>  /**
<a name="line351"></a>   * The stat
<a name="line352"></a>   * @type {goog.net.BrowserChannel.Stat}
<a name="line353"></a>   */
<a name="line354"></a>  this.stat = stat;
<a name="line355"></a>
<a name="line356"></a>};
<a name="line357"></a>goog.inherits(goog.net.BrowserChannel.StatEvent, goog.events.Event);
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Enum that identifies events for statistics that are interesting to track.
<a name="line362"></a> * TODO - Change name not to use Event or use EventTarget
<a name="line363"></a> * @enum {number}
<a name="line364"></a> */
<a name="line365"></a>goog.net.BrowserChannel.Stat = {
<a name="line366"></a>  /** Event indicating a new connection attempt. */
<a name="line367"></a>  CONNECT_ATTEMPT: 0,
<a name="line368"></a>
<a name="line369"></a>  /** Event indicating a connection error due to a general network problem. */
<a name="line370"></a>  ERROR_NETWORK: 1,
<a name="line371"></a>
<a name="line372"></a>  /**
<a name="line373"></a>   * Event indicating a connection error that isn&#39;t due to a general network
<a name="line374"></a>   * problem.
<a name="line375"></a>   */
<a name="line376"></a>  ERROR_OTHER: 2,
<a name="line377"></a>
<a name="line378"></a>  /** Event indicating the start of test stage one. */
<a name="line379"></a>  TEST_STAGE_ONE_START: 3,
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>  /** Event indicating the channel is blocked by a network administrator. */
<a name="line383"></a>  CHANNEL_BLOCKED: 4,
<a name="line384"></a>
<a name="line385"></a>  /** Event indicating the start of test stage two. */
<a name="line386"></a>  TEST_STAGE_TWO_START: 5,
<a name="line387"></a>
<a name="line388"></a>  /** Event indicating the first piece of test data was received. */
<a name="line389"></a>  TEST_STAGE_TWO_DATA_ONE: 6,
<a name="line390"></a>
<a name="line391"></a>  /**
<a name="line392"></a>   * Event indicating that the second piece of test data was received and it was
<a name="line393"></a>   * recieved separately from the first.
<a name="line394"></a>   */
<a name="line395"></a>  TEST_STAGE_TWO_DATA_TWO: 7,
<a name="line396"></a>
<a name="line397"></a>  /** Event indicating both pieces of test data were received simultaneously. */
<a name="line398"></a>  TEST_STAGE_TWO_DATA_BOTH: 8,
<a name="line399"></a>
<a name="line400"></a>  /** Event indicating stage one of the test request failed. */
<a name="line401"></a>  TEST_STAGE_ONE_FAILED: 9,
<a name="line402"></a>
<a name="line403"></a>  /** Event indicating stage two of the test request failed. */
<a name="line404"></a>  TEST_STAGE_TWO_FAILED: 10,
<a name="line405"></a>
<a name="line406"></a>  /**
<a name="line407"></a>   * Event indicating that a buffering proxy is likely between the client and
<a name="line408"></a>   * the server.
<a name="line409"></a>   */
<a name="line410"></a>  PROXY: 11,
<a name="line411"></a>
<a name="line412"></a>  /**
<a name="line413"></a>   * Event indicating that no buffering proxy is likely between the client and
<a name="line414"></a>   * the server.
<a name="line415"></a>   */
<a name="line416"></a>  NOPROXY: 12,
<a name="line417"></a>
<a name="line418"></a>  /** Event indicating an unknown SID error. */
<a name="line419"></a>  REQUEST_UNKNOWN_SESSION_ID: 13,
<a name="line420"></a>
<a name="line421"></a>  /** Event indicating a bad status code was received. */
<a name="line422"></a>  REQUEST_BAD_STATUS: 14,
<a name="line423"></a>
<a name="line424"></a>  /** Event indicating incomplete data was received */
<a name="line425"></a>  REQUEST_INCOMPLETE_DATA: 15,
<a name="line426"></a>
<a name="line427"></a>  /** Event indicating bad data was received */
<a name="line428"></a>  REQUEST_BAD_DATA: 16,
<a name="line429"></a>
<a name="line430"></a>  /** Event indicating no data was received when data was expected. */
<a name="line431"></a>  REQUEST_NO_DATA: 17,
<a name="line432"></a>
<a name="line433"></a>  /** Event indicating a request timeout. */
<a name="line434"></a>  REQUEST_TIMEOUT: 18
<a name="line435"></a>};
<a name="line436"></a>
<a name="line437"></a>
<a name="line438"></a>/**
<a name="line439"></a> * Magic response cookie that is the response for forward channel requests.
<a name="line440"></a> * @type {string}
<a name="line441"></a> */
<a name="line442"></a>goog.net.BrowserChannel.MAGIC_RESPONSE_COOKIE = &#39;y2f%&#39;;
<a name="line443"></a>
<a name="line444"></a>
<a name="line445"></a>/**
<a name="line446"></a> * Returns the browserchannel logger.
<a name="line447"></a> *
<a name="line448"></a> * @return {goog.net.ChannelDebug} The channel debug object.
<a name="line449"></a> */
<a name="line450"></a>goog.net.BrowserChannel.prototype.getChannelDebug = function() {
<a name="line451"></a>  return this.channelDebug_;
<a name="line452"></a>};
<a name="line453"></a>
<a name="line454"></a>
<a name="line455"></a>/**
<a name="line456"></a> * Set the browserchannel logger.
<a name="line457"></a> * TODO: Add interface for channel loggers or remove this function.
<a name="line458"></a> *
<a name="line459"></a> * @param {goog.net.ChannelDebug} channelDebug The channel debug object.
<a name="line460"></a> */
<a name="line461"></a>goog.net.BrowserChannel.prototype.setChannelDebug = function(
<a name="line462"></a>    channelDebug) {
<a name="line463"></a>  this.channelDebug_ = channelDebug;
<a name="line464"></a>};
<a name="line465"></a>
<a name="line466"></a>
<a name="line467"></a>/**
<a name="line468"></a> * Allows the application to set an execution hooks for when BrowserChannel
<a name="line469"></a> * starts processing requests. This is useful to track timing or logging
<a name="line470"></a> * special information. The function takes no parameters and return void.
<a name="line471"></a> * @param {Function} startHook  The function for the start hook.
<a name="line472"></a> */
<a name="line473"></a>goog.net.BrowserChannel.setStartThreadExecutionHook = function(startHook) {
<a name="line474"></a>  goog.net.BrowserChannel.startExecutionHook_ = startHook;
<a name="line475"></a>};
<a name="line476"></a>
<a name="line477"></a>
<a name="line478"></a>/**
<a name="line479"></a> * Allows the application to set an execution hooks for when BrowserChannel
<a name="line480"></a> * stops processing requests. This is useful to track timing or logging
<a name="line481"></a> * special information. The function takes no parameters and return void.
<a name="line482"></a> * @param {Function} endHook  The function for the end hook.
<a name="line483"></a> */
<a name="line484"></a>goog.net.BrowserChannel.setEndThreadExecutionHook = function(endHook) {
<a name="line485"></a>  goog.net.BrowserChannel.endExecutionHook_ = endHook;
<a name="line486"></a>};
<a name="line487"></a>
<a name="line488"></a>
<a name="line489"></a>/**
<a name="line490"></a> * Application provided execution hook for the start hook.
<a name="line491"></a> *
<a name="line492"></a> * @type {Function}
<a name="line493"></a> * @private
<a name="line494"></a> */
<a name="line495"></a>goog.net.BrowserChannel.startExecutionHook_ = function() { };
<a name="line496"></a>
<a name="line497"></a>
<a name="line498"></a>/**
<a name="line499"></a> * Application provided execution hook for the end hook.
<a name="line500"></a> *
<a name="line501"></a> * @type {Function}
<a name="line502"></a> * @private
<a name="line503"></a> */
<a name="line504"></a>goog.net.BrowserChannel.endExecutionHook_ = function() { };
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>/**
<a name="line508"></a> * Starts the channel. This initiates connections to the server.
<a name="line509"></a> *
<a name="line510"></a> * @param {string} testPath  The path for the test connection.
<a name="line511"></a> * @param {string} channelPath  The path for the channel connection.
<a name="line512"></a> * @param {Object} extraParams  Extra parameter keys and values to add to the
<a name="line513"></a> *     requests.
<a name="line514"></a> */
<a name="line515"></a>goog.net.BrowserChannel.prototype.connect = function(testPath, channelPath,
<a name="line516"></a>                                                     extraParams) {
<a name="line517"></a>  this.channelDebug_.debug(&#39;connect()&#39;);
<a name="line518"></a>
<a name="line519"></a>  goog.net.BrowserChannel.notifyStatEvent(
<a name="line520"></a>      goog.net.BrowserChannel.Stat.CONNECT_ATTEMPT);
<a name="line521"></a>
<a name="line522"></a>  this.path_ = channelPath;
<a name="line523"></a>  this.extraParams_ = extraParams;
<a name="line524"></a>  this.connectTest_(testPath);
<a name="line525"></a>};
<a name="line526"></a>
<a name="line527"></a>
<a name="line528"></a>/**
<a name="line529"></a> * Disconnects and closes the channel.
<a name="line530"></a> */
<a name="line531"></a>goog.net.BrowserChannel.prototype.disconnect = function() {
<a name="line532"></a>  this.channelDebug_.debug(&#39;disconnect()&#39;);
<a name="line533"></a>
<a name="line534"></a>  this.cancelRequests_();
<a name="line535"></a>
<a name="line536"></a>  if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line537"></a>    var rid = this.nextRid_++;
<a name="line538"></a>    var uri = this.forwardChannelUri_.clone();
<a name="line539"></a>    uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line540"></a>    uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line541"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;terminate&#39;);
<a name="line542"></a>
<a name="line543"></a>    // Add the reconnect parameters.
<a name="line544"></a>    this.addAdditionalParams_(uri);
<a name="line545"></a>
<a name="line546"></a>    var request = new goog.net.ChannelRequest(
<a name="line547"></a>        this, this.channelDebug_, this.sid_, rid);
<a name="line548"></a>    request.sendUsingImgTag(uri);
<a name="line549"></a>    this.onClose_();
<a name="line550"></a>  }
<a name="line551"></a>};
<a name="line552"></a>
<a name="line553"></a>
<a name="line554"></a>/**
<a name="line555"></a> * Returns the session id of the channel. Only available after the
<a name="line556"></a> * channel has been opened.
<a name="line557"></a> * @return {string} Session ID.
<a name="line558"></a> */
<a name="line559"></a>goog.net.BrowserChannel.prototype.getSessionId = function() {
<a name="line560"></a>  return this.sid_;
<a name="line561"></a>};
<a name="line562"></a>
<a name="line563"></a>
<a name="line564"></a>/**
<a name="line565"></a> * Starts the test channel to determine network conditions.
<a name="line566"></a> *
<a name="line567"></a> * @param {string} testPath  The relative PATH for the test connection.
<a name="line568"></a> * @private
<a name="line569"></a> */
<a name="line570"></a>goog.net.BrowserChannel.prototype.connectTest_ = function(testPath) {
<a name="line571"></a>  this.channelDebug_.debug(&#39;connectTest_()&#39;);
<a name="line572"></a>  this.connectionTest_ = new goog.net.BrowserTestChannel(
<a name="line573"></a>      this, this.channelDebug_);
<a name="line574"></a>  this.connectionTest_.setExtraHeaders(this.extraHeaders_);
<a name="line575"></a>  this.connectionTest_.connect(testPath);
<a name="line576"></a>};
<a name="line577"></a>
<a name="line578"></a>
<a name="line579"></a>/**
<a name="line580"></a> * Starts the regular channel which is run after the test channel is complete.
<a name="line581"></a> * @private
<a name="line582"></a> */
<a name="line583"></a>goog.net.BrowserChannel.prototype.connectChannel_ = function() {
<a name="line584"></a>  this.channelDebug_.debug(&#39;connectChannel_()&#39;);
<a name="line585"></a>  this.ensureInState_(goog.net.BrowserChannel.State.INIT);
<a name="line586"></a>  this.forwardChannelUri_ =
<a name="line587"></a>      this.getForwardChannelUri(/** @type {string} */ (this.path_));
<a name="line588"></a>  this.ensureForwardChannel_();
<a name="line589"></a>};
<a name="line590"></a>
<a name="line591"></a>
<a name="line592"></a>/**
<a name="line593"></a> * Cancels all outstanding requests.
<a name="line594"></a> * @private
<a name="line595"></a> */
<a name="line596"></a>goog.net.BrowserChannel.prototype.cancelRequests_ = function() {
<a name="line597"></a>  if (this.connectionTest_) {
<a name="line598"></a>    this.connectionTest_.abort();
<a name="line599"></a>    this.connectionTest_ = null;
<a name="line600"></a>  }
<a name="line601"></a>
<a name="line602"></a>  if (this.backChannelRequest_) {
<a name="line603"></a>    this.backChannelRequest_.cancel();
<a name="line604"></a>    this.backChannelRequest_ = null;
<a name="line605"></a>  }
<a name="line606"></a>
<a name="line607"></a>  if (this.forwardChannelRequest_) {
<a name="line608"></a>    this.forwardChannelRequest_.cancel();
<a name="line609"></a>    this.forwardChannelRequest_ = null;
<a name="line610"></a>  }
<a name="line611"></a>};
<a name="line612"></a>
<a name="line613"></a>
<a name="line614"></a>/**
<a name="line615"></a> * Returns the extra HTTP headers to add to all the requests sent to the server.
<a name="line616"></a> *
<a name="line617"></a> * @return {Object} The HTTP headers, or null.
<a name="line618"></a> */
<a name="line619"></a>goog.net.BrowserChannel.prototype.getExtraHeaders = function() {
<a name="line620"></a>  return this.extraHeaders_;
<a name="line621"></a>};
<a name="line622"></a>
<a name="line623"></a>
<a name="line624"></a>/**
<a name="line625"></a> * Sets extra HTTP headers to add to all the requests sent to the server.
<a name="line626"></a> *
<a name="line627"></a> * @param {Object} extraHeaders The HTTP headers, or null.
<a name="line628"></a> */
<a name="line629"></a>goog.net.BrowserChannel.prototype.setExtraHeaders = function(extraHeaders) {
<a name="line630"></a>  this.extraHeaders_ = extraHeaders;
<a name="line631"></a>};
<a name="line632"></a>
<a name="line633"></a>
<a name="line634"></a>/**
<a name="line635"></a> * Returns the handler used for channel callback events.
<a name="line636"></a> *
<a name="line637"></a> * @return {goog.net.BrowserChannel.Handler} The handler.
<a name="line638"></a> */
<a name="line639"></a>goog.net.BrowserChannel.prototype.getHandler = function() {
<a name="line640"></a>  return this.handler_;
<a name="line641"></a>};
<a name="line642"></a>
<a name="line643"></a>
<a name="line644"></a>/**
<a name="line645"></a> * Sets the handler used for channel callback events.
<a name="line646"></a> * @param {goog.net.BrowserChannel.Handler} handler The handler to set.
<a name="line647"></a> */
<a name="line648"></a>goog.net.BrowserChannel.prototype.setHandler = function(handler) {
<a name="line649"></a>  this.handler_ = handler;
<a name="line650"></a>};
<a name="line651"></a>
<a name="line652"></a>
<a name="line653"></a>/**
<a name="line654"></a> * Returns whether the channel allows the use of a subdomain. There may be
<a name="line655"></a> * cases where this isn&#39;t allowed.
<a name="line656"></a> * @return {boolean} Whether a host prefix is allowed.
<a name="line657"></a> */
<a name="line658"></a>goog.net.BrowserChannel.prototype.getAllowHostPrefix = function() {
<a name="line659"></a>  return this.allowHostPrefix_;
<a name="line660"></a>};
<a name="line661"></a>
<a name="line662"></a>
<a name="line663"></a>/**
<a name="line664"></a> * Sets whether the channel allows the use of a subdomain. There may be cases
<a name="line665"></a> * where this isn&#39;t allowed, for example, logging in with troutboard where
<a name="line666"></a> * using a subdomain causes Apache to force the user to authenticate twice.
<a name="line667"></a> * @param {boolean} allowHostPrefix Whether a host prefix is allowed.
<a name="line668"></a> */
<a name="line669"></a>goog.net.BrowserChannel.prototype.setAllowHostPrefix =
<a name="line670"></a>    function(allowHostPrefix) {
<a name="line671"></a>  this.allowHostPrefix_ = allowHostPrefix;
<a name="line672"></a>};
<a name="line673"></a>
<a name="line674"></a>/**
<a name="line675"></a> * Returns whether the channel is buffered or not. This state is valid for
<a name="line676"></a> * querying only after the test connection has completed. This may be
<a name="line677"></a> * queried in the goog.net.BrowserChannel.okToMakeRequest() callback.
<a name="line678"></a> * A channel may be buffered if the test connection determines that
<a name="line679"></a> * a chunked response could not be sent down within a suitable time.
<a name="line680"></a> * @return {boolean} Whether the channel is buffered.
<a name="line681"></a> */
<a name="line682"></a>goog.net.BrowserChannel.prototype.isBuffered = function() {
<a name="line683"></a>  return !this.useChunked_;
<a name="line684"></a>};
<a name="line685"></a>
<a name="line686"></a>/**
<a name="line687"></a> * Returns whether chunked mode is allowed. In certain debugging situations,
<a name="line688"></a> * it&#39;s useful for the application to have a way to disable chunked mode for a
<a name="line689"></a> * user.
<a name="line690"></a>
<a name="line691"></a> * @return {boolean} Whether chunked mode is allowed.
<a name="line692"></a> */
<a name="line693"></a>goog.net.BrowserChannel.prototype.getAllowChunkedMode =
<a name="line694"></a>    function() {
<a name="line695"></a>  return this.allowChunkedMode_;
<a name="line696"></a>};
<a name="line697"></a>
<a name="line698"></a>
<a name="line699"></a>/**
<a name="line700"></a> * Sets whether chunked mode is allowed. In certain debugging situations, it&#39;s
<a name="line701"></a> * useful for the application to have a way to disable chunked mode for a user.
<a name="line702"></a> * @param {boolean} allowChunkedMode  Whether chunked mode is allowed.
<a name="line703"></a> */
<a name="line704"></a>goog.net.BrowserChannel.prototype.setAllowChunkedMode =
<a name="line705"></a>    function(allowChunkedMode) {
<a name="line706"></a>  this.allowChunkedMode_ = allowChunkedMode;
<a name="line707"></a>};
<a name="line708"></a>
<a name="line709"></a>
<a name="line710"></a>/**
<a name="line711"></a> * Sends a request to the server. The format of the request is a Map data
<a name="line712"></a> * structure of key/value pairs. These maps are then encoded in a format
<a name="line713"></a> * suitable for the wire and then reconstituted as a Map data structure that
<a name="line714"></a> * the server can process.
<a name="line715"></a> * @param {Object|goog.structs.Map} map  The map to send.
<a name="line716"></a> */
<a name="line717"></a>goog.net.BrowserChannel.prototype.sendMap = function(map) {
<a name="line718"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
<a name="line719"></a>    throw Error(&#39;Invalid operation: sending map when state is closed&#39;);
<a name="line720"></a>  }
<a name="line721"></a>
<a name="line722"></a>  this.outgoingMaps_.push(map);
<a name="line723"></a>  if (this.state_ == goog.net.BrowserChannel.State.OPENING ||
<a name="line724"></a>      this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line725"></a>    this.ensureForwardChannel_();
<a name="line726"></a>  }
<a name="line727"></a>};
<a name="line728"></a>
<a name="line729"></a>
<a name="line730"></a>/**
<a name="line731"></a> * When set to true, this changes the behavior of the forward channel so it
<a name="line732"></a> * will not retry requests; it will fail after one network failure, and if
<a name="line733"></a> * there was already one network failure, the request will fail immediately.
<a name="line734"></a> * @param {boolean} failFast  Whether or not to fail fast.
<a name="line735"></a> */
<a name="line736"></a>goog.net.BrowserChannel.prototype.setFailFast = function(failFast) {
<a name="line737"></a>  this.failFast_ = failFast;
<a name="line738"></a>  this.channelDebug_.info(&#39;setFailFast: &#39; + failFast);
<a name="line739"></a>  if (this.forwardChannelRequest_) {
<a name="line740"></a>    this.channelDebug_.info(&#39;setting max forward-channel retries to &#39; +
<a name="line741"></a>        this.getForwardChannelMaxRetries());
<a name="line742"></a>    this.forwardChannelRequest_.setMaxRetries(
<a name="line743"></a>        this.getForwardChannelMaxRetries());
<a name="line744"></a>  }
<a name="line745"></a>};
<a name="line746"></a>
<a name="line747"></a>
<a name="line748"></a>/**
<a name="line749"></a> * @return {number}  The max number of retries, which will be 0 in fail-fast
<a name="line750"></a> * mode.
<a name="line751"></a> */
<a name="line752"></a>goog.net.BrowserChannel.prototype.getForwardChannelMaxRetries = function() {
<a name="line753"></a>  return this.failFast_ ?
<a name="line754"></a>         0 : goog.net.BrowserChannel.FORWARD_CHANNEL_MAX_RETRIES;
<a name="line755"></a>};
<a name="line756"></a>
<a name="line757"></a>
<a name="line758"></a>/**
<a name="line759"></a> * Returns whether the channel is closed
<a name="line760"></a> * @return {boolean} true if the channel is closed.
<a name="line761"></a> */
<a name="line762"></a>goog.net.BrowserChannel.prototype.isClosed = function() {
<a name="line763"></a>  return this.state_ == goog.net.BrowserChannel.State.CLOSED;
<a name="line764"></a>};
<a name="line765"></a>
<a name="line766"></a>
<a name="line767"></a>/**
<a name="line768"></a> * Returns the browser channel state.
<a name="line769"></a> * @return {goog.net.BrowserChannel.State} The current state of the browser
<a name="line770"></a> * channel.
<a name="line771"></a> */
<a name="line772"></a>goog.net.BrowserChannel.prototype.getState = function() {
<a name="line773"></a>  return this.state_;
<a name="line774"></a>};
<a name="line775"></a>
<a name="line776"></a>/**
<a name="line777"></a> * Return the last status code received for a request.
<a name="line778"></a> * @return {number} The last status code received for a request.
<a name="line779"></a> */
<a name="line780"></a>goog.net.BrowserChannel.prototype.getLastStatusCode = function() {
<a name="line781"></a>  return this.lastStatusCode_;
<a name="line782"></a>};
<a name="line783"></a>
<a name="line784"></a>
<a name="line785"></a>/**
<a name="line786"></a> * Returns whether there are outstanding requests servicing the channel.
<a name="line787"></a> * @return {boolean} true if there are outstanding requests.
<a name="line788"></a> */
<a name="line789"></a>goog.net.BrowserChannel.prototype.hasOutstandingRequests = function() {
<a name="line790"></a>  return this.outstandingRequests_() != 0;
<a name="line791"></a>};
<a name="line792"></a>
<a name="line793"></a>
<a name="line794"></a>/**
<a name="line795"></a> * Returns the number of outstanding requests.
<a name="line796"></a> * @return {number} The number of outstanding requests to the server.
<a name="line797"></a> * @private
<a name="line798"></a> */
<a name="line799"></a>goog.net.BrowserChannel.prototype.outstandingRequests_ = function() {
<a name="line800"></a>  var count = 0;
<a name="line801"></a>  if (this.backChannelRequest_) {
<a name="line802"></a>    count++;
<a name="line803"></a>  }
<a name="line804"></a>  if (this.forwardChannelRequest_) {
<a name="line805"></a>    count++;
<a name="line806"></a>  }
<a name="line807"></a>  return count;
<a name="line808"></a>};
<a name="line809"></a>
<a name="line810"></a>
<a name="line811"></a>/**
<a name="line812"></a> * Ensures that a forward channel request is scheduled.
<a name="line813"></a> * @private
<a name="line814"></a> */
<a name="line815"></a>goog.net.BrowserChannel.prototype.ensureForwardChannel_ = function() {
<a name="line816"></a>  if (this.forwardChannelRequest_) {
<a name="line817"></a>    // connection in process - no need to start a new request
<a name="line818"></a>    return;
<a name="line819"></a>  }
<a name="line820"></a>
<a name="line821"></a>  if (this.forwardChannelTimerId_) {
<a name="line822"></a>    // no need to start a new request - one is already scheduled
<a name="line823"></a>    return;
<a name="line824"></a>  }
<a name="line825"></a>
<a name="line826"></a>  this.forwardChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line827"></a>      goog.bind(this.onStartForwardChannelTimer_, this), 0);
<a name="line828"></a>};
<a name="line829"></a>
<a name="line830"></a>
<a name="line831"></a>/**
<a name="line832"></a> * Timer callback for ensureForwardChannel
<a name="line833"></a> * @private
<a name="line834"></a> */
<a name="line835"></a>goog.net.BrowserChannel.prototype.onStartForwardChannelTimer_ = function() {
<a name="line836"></a>  this.forwardChannelTimerId_ = null;
<a name="line837"></a>  this.startForwardChannel_();
<a name="line838"></a>};
<a name="line839"></a>
<a name="line840"></a>
<a name="line841"></a>/**
<a name="line842"></a> * Begins a new forward channel operation to the server.
<a name="line843"></a> * @private
<a name="line844"></a> */
<a name="line845"></a>goog.net.BrowserChannel.prototype.startForwardChannel_ = function() {
<a name="line846"></a>  this.channelDebug_.debug(&#39;startForwardChannel_&#39;);
<a name="line847"></a>
<a name="line848"></a>  if (this.state_ == goog.net.BrowserChannel.State.INIT) {
<a name="line849"></a>    this.open_();
<a name="line850"></a>    this.state_ = goog.net.BrowserChannel.State.OPENING;
<a name="line851"></a>  } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line852"></a>    // make sure there is a connection open to receive data
<a name="line853"></a>    this.ensureBackChannel_();
<a name="line854"></a>
<a name="line855"></a>    if (!this.okToMakeRequest_()) {
<a name="line856"></a>      // channel is cancelled
<a name="line857"></a>      return;
<a name="line858"></a>    }
<a name="line859"></a>
<a name="line860"></a>    if (this.outgoingMaps_.length == 0) {
<a name="line861"></a>      this.channelDebug_.debug(&#39;startForwardChannel_ returned: &#39; +
<a name="line862"></a>                                  &#39;nothing to send&#39;);
<a name="line863"></a>      // no need to start a new forward channel request
<a name="line864"></a>      return;
<a name="line865"></a>    }
<a name="line866"></a>
<a name="line867"></a>    if (this.forwardChannelRequest_) {
<a name="line868"></a>      this.channelDebug_.debug(&#39;startForwardChannel_ returned: &#39; +
<a name="line869"></a>                                  &#39;connection already in progress&#39;);
<a name="line870"></a>      // no need to start a new forward channel request
<a name="line871"></a>      return;
<a name="line872"></a>    }
<a name="line873"></a>
<a name="line874"></a>    this.makeForwardChannelRequest_();
<a name="line875"></a>    this.channelDebug_.debug(&#39;startForwardChannel_ finished, sent request&#39;);
<a name="line876"></a>  }
<a name="line877"></a>};
<a name="line878"></a>
<a name="line879"></a>
<a name="line880"></a>/**
<a name="line881"></a> * Establishes a new channel session with the the server.
<a name="line882"></a> * @private
<a name="line883"></a> */
<a name="line884"></a>goog.net.BrowserChannel.prototype.open_ = function() {
<a name="line885"></a>  this.channelDebug_.debug(&#39;open_()&#39;);
<a name="line886"></a>  this.state_ = goog.net.BrowserChannel.State.OPENING;
<a name="line887"></a>  this.nextRid_ = Math.floor(Math.random() * 100000);
<a name="line888"></a>
<a name="line889"></a>  var rid = this.nextRid_++;
<a name="line890"></a>  var request = new goog.net.ChannelRequest(this, this.channelDebug_, &#39;&#39;, rid);
<a name="line891"></a>  request.setExtraHeaders(this.extraHeaders_);
<a name="line892"></a>  var requestText = this.dequeueOutgoingMaps_();
<a name="line893"></a>  var uri = this.forwardChannelUri_.clone();
<a name="line894"></a>  uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line895"></a>  if (this.clientVersion_) {
<a name="line896"></a>   uri.setParameterValue(&#39;CVER&#39;, this.clientVersion_);
<a name="line897"></a>  }
<a name="line898"></a>
<a name="line899"></a>  // Add the reconnect parameters.
<a name="line900"></a>  this.addAdditionalParams_(uri);
<a name="line901"></a>
<a name="line902"></a>  request.xmlHttpPost(uri, requestText, true);
<a name="line903"></a>  this.forwardChannelRequest_ = request;
<a name="line904"></a>};
<a name="line905"></a>
<a name="line906"></a>
<a name="line907"></a>/**
<a name="line908"></a> * Makes a forward channel request using XMLHTTP.
<a name="line909"></a> * @private
<a name="line910"></a> */
<a name="line911"></a>goog.net.BrowserChannel.prototype.makeForwardChannelRequest_ = function() {
<a name="line912"></a>  var rid = this.nextRid_++;
<a name="line913"></a>  var uri = this.forwardChannelUri_.clone();
<a name="line914"></a>  uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line915"></a>  uri.setParameterValue(&#39;RID&#39;, rid);
<a name="line916"></a>  // Add the additional reconnect parameters.
<a name="line917"></a>  this.addAdditionalParams_(uri);
<a name="line918"></a>
<a name="line919"></a>  var requestText = this.dequeueOutgoingMaps_();
<a name="line920"></a>  var request = new goog.net.ChannelRequest(
<a name="line921"></a>      this, this.channelDebug_, this.sid_, rid);
<a name="line922"></a>  request.setExtraHeaders(this.extraHeaders_);
<a name="line923"></a>  request.setMaxRetries(this.getForwardChannelMaxRetries());
<a name="line924"></a>
<a name="line925"></a>  // randomize from 50%-100% of the forward channel retry timeout to avoid
<a name="line926"></a>  // a big hit if servers happen to die at once.
<a name="line927"></a>  request.setRetryTimeout(
<a name="line928"></a>      Math.round(goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT * 0.50) +
<a name="line929"></a>      Math.round(goog.net.BrowserChannel.FORWARD_CHANNEL_RETRY_TIMEOUT * 0.50 *
<a name="line930"></a>                 Math.random()));
<a name="line931"></a>  this.forwardChannelRequest_ = request;
<a name="line932"></a>  request.xmlHttpPost(uri, requestText, true);
<a name="line933"></a>};
<a name="line934"></a>
<a name="line935"></a>
<a name="line936"></a>/**
<a name="line937"></a> * Adds the additional parameters from the handler to the given URI.
<a name="line938"></a> * @param {goog.Uri} uri The URI to add the parameters to.
<a name="line939"></a> * @private
<a name="line940"></a> */
<a name="line941"></a>goog.net.BrowserChannel.prototype.addAdditionalParams_ = function(uri) {
<a name="line942"></a>  // Add the additional reconnect parameters as needed.
<a name="line943"></a>  if (this.handler_) {
<a name="line944"></a>    var params = this.handler_.getAdditionalParams(this);
<a name="line945"></a>    if (params) {
<a name="line946"></a>      goog.structs.forEach(params, function(value, key, coll) {
<a name="line947"></a>        uri.setParameterValue(key, value);
<a name="line948"></a>      });
<a name="line949"></a>    }
<a name="line950"></a>  }
<a name="line951"></a>};
<a name="line952"></a>
<a name="line953"></a>
<a name="line954"></a>/**
<a name="line955"></a> * Returns the request text from the outgoing maps and resets it.
<a name="line956"></a> * @return {string} The encoded request text created from all the currently
<a name="line957"></a> *                  queued outgoing maps.
<a name="line958"></a> * @private
<a name="line959"></a> */
<a name="line960"></a>goog.net.BrowserChannel.prototype.dequeueOutgoingMaps_ = function() {
<a name="line961"></a>  var sb = [&#39;count=&#39; + this.outgoingMaps_.length];
<a name="line962"></a>  for (var i = 0; i &lt; this.outgoingMaps_.length; i++) {
<a name="line963"></a>    var map = this.outgoingMaps_[i];
<a name="line964"></a>    goog.structs.forEach(map, function(value, key, coll) {
<a name="line965"></a>      sb.push(&#39;req&#39; + i + &#39;_&#39; + key + &#39;=&#39; + encodeURIComponent(value));
<a name="line966"></a>    });
<a name="line967"></a>  }
<a name="line968"></a>  this.outgoingMaps_.length = 0;
<a name="line969"></a>  return sb.join(&#39;&amp;&#39;);
<a name="line970"></a>};
<a name="line971"></a>
<a name="line972"></a>
<a name="line973"></a>/**
<a name="line974"></a> * Ensures there is a backchannel request for receiving data from the server.
<a name="line975"></a> * @private
<a name="line976"></a> */
<a name="line977"></a>goog.net.BrowserChannel.prototype.ensureBackChannel_ = function() {
<a name="line978"></a>  if (this.backChannelRequest_) {
<a name="line979"></a>    // already have one
<a name="line980"></a>    return;
<a name="line981"></a>  }
<a name="line982"></a>
<a name="line983"></a>  if (this.backChannelTimerId_) {
<a name="line984"></a>    // no need to start a new request - one is already scheduled
<a name="line985"></a>    return;
<a name="line986"></a>  }
<a name="line987"></a>
<a name="line988"></a>  this.backChannelTimerId_ = goog.net.BrowserChannel.setTimeout(
<a name="line989"></a>      goog.bind(this.onStartBackChannelTimer_, this), 0);
<a name="line990"></a>};
<a name="line991"></a>
<a name="line992"></a>
<a name="line993"></a>/**
<a name="line994"></a> * Timer callback for ensureBackChannel_.
<a name="line995"></a> * @private
<a name="line996"></a> */
<a name="line997"></a>goog.net.BrowserChannel.prototype.onStartBackChannelTimer_ = function() {
<a name="line998"></a>  this.backChannelTimerId_ = null;
<a name="line999"></a>  this.startBackChannel_();
<a name="line1000"></a>};
<a name="line1001"></a>
<a name="line1002"></a>
<a name="line1003"></a>/**
<a name="line1004"></a> * Begins a new back channel operation to the server.
<a name="line1005"></a> * @private
<a name="line1006"></a> */
<a name="line1007"></a>goog.net.BrowserChannel.prototype.startBackChannel_ = function() {
<a name="line1008"></a>  if (!this.okToMakeRequest_()) {
<a name="line1009"></a>    // channel is cancelled
<a name="line1010"></a>    return;
<a name="line1011"></a>  }
<a name="line1012"></a>
<a name="line1013"></a>  this.channelDebug_.debug(&#39;Creating new HttpRequest&#39;);
<a name="line1014"></a>  this.backChannelRequest_ =
<a name="line1015"></a>      new goog.net.ChannelRequest(this, this.channelDebug_, this.sid_, &#39;rpc&#39;);
<a name="line1016"></a>  this.backChannelRequest_.setExtraHeaders(this.extraHeaders_);
<a name="line1017"></a>  var uri = this.backChannelUri_.clone();
<a name="line1018"></a>  uri.setParameterValue(&#39;RID&#39;, &#39;rpc&#39;);
<a name="line1019"></a>  uri.setParameterValue(&#39;SID&#39;, this.sid_);
<a name="line1020"></a>  uri.setParameterValue(&#39;CI&#39;, this.useChunked_ ? &#39;0&#39; : &#39;1&#39;);
<a name="line1021"></a>  uri.setParameterValue(&#39;AID&#39;, this.lastArrayId_);
<a name="line1022"></a>
<a name="line1023"></a>  // Add the reconnect parameters.
<a name="line1024"></a>  this.addAdditionalParams_(uri);
<a name="line1025"></a>
<a name="line1026"></a>  if (goog.userAgent.IE) {
<a name="line1027"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;html&#39;);
<a name="line1028"></a>    this.backChannelRequest_.tridentGet(uri, Boolean(this.hostPrefix_));
<a name="line1029"></a>  } else {
<a name="line1030"></a>    uri.setParameterValue(&#39;TYPE&#39;, &#39;xmlhttp&#39;);
<a name="line1031"></a>    this.backChannelRequest_.setMaxRetries(
<a name="line1032"></a>        goog.net.BrowserChannel.BACK_CHANNEL_XMHTTP_MAX_RETRIES);
<a name="line1033"></a>    this.backChannelRequest_.xmlHttpGet(uri, true);
<a name="line1034"></a>  }
<a name="line1035"></a>  this.channelDebug_.debug(&#39;New Request created&#39;);
<a name="line1036"></a>};
<a name="line1037"></a>
<a name="line1038"></a>
<a name="line1039"></a>/**
<a name="line1040"></a> * Gives the handler a chance to return an error code and stop channel
<a name="line1041"></a> * execution. A handler might want to do this to check that the user is still
<a name="line1042"></a> * logged in, for example.
<a name="line1043"></a> * @private
<a name="line1044"></a> * @return {boolean} If it&#39;s OK to make a request.
<a name="line1045"></a> */
<a name="line1046"></a>goog.net.BrowserChannel.prototype.okToMakeRequest_ = function() {
<a name="line1047"></a>  if (this.handler_) {
<a name="line1048"></a>    var result = this.handler_.okToMakeRequest(this);
<a name="line1049"></a>    if (result != goog.net.BrowserChannel.Error.OK) {
<a name="line1050"></a>      this.channelDebug_.debug(&#39;Handler returned error code from &#39; +
<a name="line1051"></a>                                  &#39;okToMakeRequest&#39;);
<a name="line1052"></a>      this.signalError_(result);
<a name="line1053"></a>      return false;
<a name="line1054"></a>    }
<a name="line1055"></a>  }
<a name="line1056"></a>  return true;
<a name="line1057"></a>};
<a name="line1058"></a>
<a name="line1059"></a>
<a name="line1060"></a>/**
<a name="line1061"></a> * Callback from BrowserTestChannel for when the channel is finished.
<a name="line1062"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1063"></a> * @param {boolean} useChunked  Whether we can chunk responses.
<a name="line1064"></a> */
<a name="line1065"></a>goog.net.BrowserChannel.prototype.testConnectionFinished =
<a name="line1066"></a>    function(testChannel, useChunked) {
<a name="line1067"></a>  this.channelDebug_.debug(&#39;Test Connection Finished&#39;);
<a name="line1068"></a>
<a name="line1069"></a>  this.useChunked_ = this.allowChunkedMode_ &amp;&amp; useChunked;
<a name="line1070"></a>  this.lastStatusCode_ = testChannel.getLastStatusCode();
<a name="line1071"></a>  this.connectChannel_();
<a name="line1072"></a>};
<a name="line1073"></a>
<a name="line1074"></a>
<a name="line1075"></a>/**
<a name="line1076"></a> * Callback from BrowserTestChannel for when the channel has an error.
<a name="line1077"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1078"></a> * @param {goog.net.ChannelRequest.Error} errorCode  The error code of the
<a name="line1079"></a>       failure.
<a name="line1080"></a> */
<a name="line1081"></a>goog.net.BrowserChannel.prototype.testConnectionFailure =
<a name="line1082"></a>    function(testChannel, errorCode) {
<a name="line1083"></a>  this.channelDebug_.debug(&#39;Test Connection Failed&#39;);
<a name="line1084"></a>  this.lastStatusCode_ = testChannel.getLastStatusCode();
<a name="line1085"></a>  this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
<a name="line1086"></a>};
<a name="line1087"></a>
<a name="line1088"></a>
<a name="line1089"></a>/**
<a name="line1090"></a> * Callback from BrowserTestChannel for when the channel is blocked.
<a name="line1091"></a> * @param {goog.net.BrowserTestChannel} testChannel The BrowserTestChannel.
<a name="line1092"></a> */
<a name="line1093"></a>goog.net.BrowserChannel.prototype.testConnectionBlocked =
<a name="line1094"></a>    function(testChannel) {
<a name="line1095"></a>  this.channelDebug_.debug(&#39;Test Connection Blocked&#39;);
<a name="line1096"></a>  this.lastStatusCode_ = this.connectionTest_.getLastStatusCode();
<a name="line1097"></a>  this.signalError_(goog.net.BrowserChannel.Error.BLOCKED);
<a name="line1098"></a>};
<a name="line1099"></a>
<a name="line1100"></a>
<a name="line1101"></a>/**
<a name="line1102"></a> * Callback from ChannelRequest for when new data is received
<a name="line1103"></a> * @param {goog.net.ChannelRequest} request  The request object.
<a name="line1104"></a> * @param {string} responseText The text of the response.
<a name="line1105"></a> */
<a name="line1106"></a>goog.net.BrowserChannel.prototype.onRequestData =
<a name="line1107"></a>    function(request, responseText) {
<a name="line1108"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED ||
<a name="line1109"></a>      (this.backChannelRequest_ != request &amp;&amp;
<a name="line1110"></a>       this.forwardChannelRequest_ != request)) {
<a name="line1111"></a>    // either CLOSED or a request we don&#39;t know about (perhaps an old request)
<a name="line1112"></a>    return;
<a name="line1113"></a>  }
<a name="line1114"></a>  this.lastStatusCode_ = request.getLastStatusCode();
<a name="line1115"></a>
<a name="line1116"></a>  if (this.forwardChannelRequest_ == request &amp;&amp;
<a name="line1117"></a>      this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line1118"></a>    // expect magic cookie in response
<a name="line1119"></a>    if (responseText != goog.net.BrowserChannel.MAGIC_RESPONSE_COOKIE) {
<a name="line1120"></a>      this.channelDebug_.debug(&#39;Bad data returned - missing/invald &#39; +
<a name="line1121"></a>                                  &#39;magic cookie&#39;);
<a name="line1122"></a>      this.signalError_(goog.net.BrowserChannel.Error.BAD_RESPONSE);
<a name="line1123"></a>    }
<a name="line1124"></a>  } else {
<a name="line1125"></a>    if (!goog.string.isEmpty(responseText)) {
<a name="line1126"></a>      this.onInput_(/** @type {Array} */ (goog.json.unsafeParse(responseText)));
<a name="line1127"></a>    }
<a name="line1128"></a>  }
<a name="line1129"></a>};
<a name="line1130"></a>
<a name="line1131"></a>
<a name="line1132"></a>/**
<a name="line1133"></a> * Callback from ChannelRequest that indicates a request has completed.
<a name="line1134"></a> * @param {goog.net.ChannelRequest} request  The request object.
<a name="line1135"></a> */
<a name="line1136"></a>goog.net.BrowserChannel.prototype.onRequestComplete =
<a name="line1137"></a>    function(request) {
<a name="line1138"></a>  this.channelDebug_.debug(&#39;Request complete&#39;);
<a name="line1139"></a>  var foundRequest = false;
<a name="line1140"></a>  if (this.backChannelRequest_ == request) {
<a name="line1141"></a>    this.backChannelRequest_ = null;
<a name="line1142"></a>    foundRequest = true;
<a name="line1143"></a>  } else if (this.forwardChannelRequest_ == request) {
<a name="line1144"></a>    this.forwardChannelRequest_ = null;
<a name="line1145"></a>    foundRequest = true;
<a name="line1146"></a>  }
<a name="line1147"></a>
<a name="line1148"></a>  // return if it was an old request from a previous session
<a name="line1149"></a>  if (!foundRequest) {
<a name="line1150"></a>    return;
<a name="line1151"></a>  }
<a name="line1152"></a>  this.lastStatusCode_ = request.getLastStatusCode();
<a name="line1153"></a>
<a name="line1154"></a>  if (this.state_ == goog.net.BrowserChannel.State.CLOSED) {
<a name="line1155"></a>    return;
<a name="line1156"></a>  }
<a name="line1157"></a>
<a name="line1158"></a>  if (!request.getSuccess()) {
<a name="line1159"></a>    this.channelDebug_.debug(&#39;Error: HTTP request failed&#39;);
<a name="line1160"></a>    switch (request.getLastError()) {
<a name="line1161"></a>      case goog.net.ChannelRequest.Error.NO_DATA:
<a name="line1162"></a>        this.signalError_(goog.net.BrowserChannel.Error.NO_DATA);
<a name="line1163"></a>        break;
<a name="line1164"></a>      case goog.net.ChannelRequest.Error.BAD_DATA:
<a name="line1165"></a>        this.signalError_(goog.net.BrowserChannel.Error.BAD_DATA);
<a name="line1166"></a>        break;
<a name="line1167"></a>      case goog.net.ChannelRequest.Error.UNKNOWN_SESSION_ID:
<a name="line1168"></a>        this.signalError_(goog.net.BrowserChannel.Error.UNKNOWN_SESSION_ID);
<a name="line1169"></a>        break;
<a name="line1170"></a>      default:
<a name="line1171"></a>        this.signalError_(goog.net.BrowserChannel.Error.REQUEST_FAILED);
<a name="line1172"></a>        break;
<a name="line1173"></a>    }
<a name="line1174"></a>    return;
<a name="line1175"></a>  }
<a name="line1176"></a>
<a name="line1177"></a>  this.ensureForwardChannel_();
<a name="line1178"></a>  this.ensureBackChannel_();
<a name="line1179"></a>};
<a name="line1180"></a>
<a name="line1181"></a>
<a name="line1182"></a>/**
<a name="line1183"></a> * Processes the data returned by the server.
<a name="line1184"></a> * @param {Array} respArray The response array returned by the server.
<a name="line1185"></a> * @private
<a name="line1186"></a> */
<a name="line1187"></a>goog.net.BrowserChannel.prototype.onInput_ = function(respArray) {
<a name="line1188"></a>  // respArray is an array of arrays
<a name="line1189"></a>  var batch = this.handler_ &amp;&amp; this.handler_.channelHandleMultipleArrays ?
<a name="line1190"></a>      [] : null;
<a name="line1191"></a>  for (var i = 0; i &lt; respArray.length; i++) {
<a name="line1192"></a>    var nextArray = respArray[i];
<a name="line1193"></a>    this.lastArrayId_ = nextArray[0];
<a name="line1194"></a>    nextArray = nextArray[1];
<a name="line1195"></a>    if (this.state_ == goog.net.BrowserChannel.State.OPENING) {
<a name="line1196"></a>      if (nextArray[0] == &#39;c&#39;) {
<a name="line1197"></a>        this.sid_ = nextArray[1];
<a name="line1198"></a>        if (this.allowHostPrefix_) {
<a name="line1199"></a>          this.hostPrefix_ = nextArray[2];
<a name="line1200"></a>        } else {
<a name="line1201"></a>          this.hostPrefix_ = null;
<a name="line1202"></a>        }
<a name="line1203"></a>        this.state_ = goog.net.BrowserChannel.State.OPENED;
<a name="line1204"></a>        if (this.handler_) {
<a name="line1205"></a>          this.handler_.channelOpened(this);
<a name="line1206"></a>        }
<a name="line1207"></a>        this.backChannelUri_ = this.getBackChannelUri(
<a name="line1208"></a>            this.hostPrefix_, /** @type {string} */ (this.path_));
<a name="line1209"></a>      } else if (nextArray[0] == &#39;stop&#39;) {
<a name="line1210"></a>        this.signalError_(goog.net.BrowserChannel.Error.STOP);
<a name="line1211"></a>      }
<a name="line1212"></a>    } else if (this.state_ == goog.net.BrowserChannel.State.OPENED) {
<a name="line1213"></a>      if (nextArray[0] == &#39;stop&#39;) {
<a name="line1214"></a>        if (batch &amp;&amp; batch.length) {
<a name="line1215"></a>          this.handler_.channelHandleMultipleArrays(this, batch);
<a name="line1216"></a>          batch.length = 0;
<a name="line1217"></a>        }
<a name="line1218"></a>        this.signalError_(goog.net.BrowserChannel.Error.STOP);
<a name="line1219"></a>      } else if (nextArray[0] == &#39;noop&#39;) {
<a name="line1220"></a>        // ignore - noop to keep connection happy
<a name="line1221"></a>      } else {
<a name="line1222"></a>        if (batch) {
<a name="line1223"></a>          batch.push(nextArray);
<a name="line1224"></a>        } else if (this.handler_) {
<a name="line1225"></a>          this.handler_.channelHandleArray(this, nextArray);
<a name="line1226"></a>        }
<a name="line1227"></a>      }
<a name="line1228"></a>    }
<a name="line1229"></a>  }
<a name="line1230"></a>  if (batch &amp;&amp; batch.length) {
<a name="line1231"></a>    this.handler_.channelHandleMultipleArrays(this, batch);
<a name="line1232"></a>  }
<a name="line1233"></a>};
<a name="line1234"></a>
<a name="line1235"></a>
<a name="line1236"></a>/**
<a name="line1237"></a> * Helper to ensure the BrowserChannel is in the expected state.
<a name="line1238"></a> * @param {number} state The expected state.
<a name="line1239"></a> * @private
<a name="line1240"></a> */
<a name="line1241"></a>goog.net.BrowserChannel.prototype.ensureInState_ = function(state) {
<a name="line1242"></a>  if (this.state_ != state) {
<a name="line1243"></a>    throw Error(&#39;Invalid operation: expected channel state &#39; + state +
<a name="line1244"></a>      &#39; got channel state &#39; + this.state_);
<a name="line1245"></a>  }
<a name="line1246"></a>};
<a name="line1247"></a>
<a name="line1248"></a>
<a name="line1249"></a>/**
<a name="line1250"></a> * Signals an error has occurred.
<a name="line1251"></a> * @param {goog.net.BrowserChannel.Error} error  The error code for the failure.
<a name="line1252"></a> * @private
<a name="line1253"></a> */
<a name="line1254"></a>goog.net.BrowserChannel.prototype.signalError_ = function(error) {
<a name="line1255"></a>  this.channelDebug_.info(&#39;Error code &#39; + error);
<a name="line1256"></a>  if (error == goog.net.BrowserChannel.Error.REQUEST_FAILED ||
<a name="line1257"></a>      error == goog.net.BrowserChannel.Error.BLOCKED) {
<a name="line1258"></a>    // Ping google to check if it&#39;s a gmail error or user&#39;s network error
<a name="line1259"></a>    var imageUri = null;
<a name="line1260"></a>    if (this.handler_) {
<a name="line1261"></a>      imageUri = this.handler_.getNetworkTestImageUri(this);
<a name="line1262"></a>    }
<a name="line1263"></a>    goog.net.testGoogleCom(
<a name="line1264"></a>        goog.bind(this.testGoogleComCallback_, this, error), imageUri);
<a name="line1265"></a>  } else {
<a name="line1266"></a>    this.onError_(error);
<a name="line1267"></a>  }
<a name="line1268"></a>};
<a name="line1269"></a>
<a name="line1270"></a>
<a name="line1271"></a>/**
<a name="line1272"></a> * Callback for testGoogleCom during error handling.
<a name="line1273"></a> * @param {goog.net.BrowserChannel.Error} error The error code for the failure.
<a name="line1274"></a> * @param {boolean} networkUp Whether the network is up.
<a name="line1275"></a> * @private
<a name="line1276"></a> */
<a name="line1277"></a>goog.net.BrowserChannel.prototype.testGoogleComCallback_ =
<a name="line1278"></a>    function(error, networkUp) {
<a name="line1279"></a>  if (networkUp) {
<a name="line1280"></a>    this.channelDebug_.info(&#39;Successfully pinged google.com&#39;);
<a name="line1281"></a>    this.onError_(error);
<a name="line1282"></a>  } else {
<a name="line1283"></a>    this.channelDebug_.info(&#39;Failed to ping google.com&#39;);
<a name="line1284"></a>    this.onError_(goog.net.BrowserChannel.Error.NETWORK);
<a name="line1285"></a>  }
<a name="line1286"></a>};
<a name="line1287"></a>
<a name="line1288"></a>
<a name="line1289"></a>/**
<a name="line1290"></a> * Called when we&#39;ve determined the final error for a channel. It closes the
<a name="line1291"></a> * notifiers the handler of the error and closes the channel.
<a name="line1292"></a> * @param {goog.net.BrowserChannel.Error} error  The error code for the failure.
<a name="line1293"></a> * @private
<a name="line1294"></a> */
<a name="line1295"></a>goog.net.BrowserChannel.prototype.onError_ = function(error) {
<a name="line1296"></a>  this.channelDebug_.debug(&#39;HttpChannel: error - &#39; + error);
<a name="line1297"></a>  if (error == goog.net.BrowserChannel.Error.NETWORK) {
<a name="line1298"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line1299"></a>        goog.net.BrowserChannel.Stat.ERROR_NETWORK);
<a name="line1300"></a>  } else {
<a name="line1301"></a>    goog.net.BrowserChannel.notifyStatEvent(
<a name="line1302"></a>        goog.net.BrowserChannel.Stat.ERROR_OTHER);
<a name="line1303"></a>  }
<a name="line1304"></a>  this.state_ = goog.net.BrowserChannel.State.CLOSED;
<a name="line1305"></a>  if (this.handler_) {
<a name="line1306"></a>    this.handler_.channelError(this, error);
<a name="line1307"></a>  }
<a name="line1308"></a>  this.onClose_();
<a name="line1309"></a>  this.cancelRequests_();
<a name="line1310"></a>};
<a name="line1311"></a>
<a name="line1312"></a>
<a name="line1313"></a>/**
<a name="line1314"></a> * Called when the channel has been closed. It notifiers the handler of the
<a name="line1315"></a> * event.
<a name="line1316"></a> * @private
<a name="line1317"></a> */
<a name="line1318"></a>goog.net.BrowserChannel.prototype.onClose_ = function() {
<a name="line1319"></a>  this.state_ = goog.net.BrowserChannel.State.CLOSED;
<a name="line1320"></a>  this.lastStatusCode_ = -1;
<a name="line1321"></a>  if (this.handler_) {
<a name="line1322"></a>    this.handler_.channelClosed(this);
<a name="line1323"></a>  }
<a name="line1324"></a>};
<a name="line1325"></a>
<a name="line1326"></a>
<a name="line1327"></a>/**
<a name="line1328"></a> * Gets the Uri used for the connection that sends data to the server.
<a name="line1329"></a> * @param {string} path The path on the host.
<a name="line1330"></a> * @return {goog.Uri} The forward channel URI.
<a name="line1331"></a> */
<a name="line1332"></a>goog.net.BrowserChannel.prototype.getForwardChannelUri =
<a name="line1333"></a>    function(path) {
<a name="line1334"></a>  var uri = this.createDataUri(null, path);
<a name="line1335"></a>  this.channelDebug_.debug(&#39;GetForwardChannelUri: &#39; + uri);
<a name="line1336"></a>  return uri;
<a name="line1337"></a>};
<a name="line1338"></a>
<a name="line1339"></a>
<a name="line1340"></a>/**
<a name="line1341"></a> * Gets the Uri used for the connection that receives data from the server.
<a name="line1342"></a> * @param {string?} hostPrefix The host prefix.
<a name="line1343"></a> * @param {string} path The path on the host.
<a name="line1344"></a> * @return {goog.Uri} The back channel URI.
<a name="line1345"></a> */
<a name="line1346"></a>goog.net.BrowserChannel.prototype.getBackChannelUri =
<a name="line1347"></a>    function(hostPrefix, path) {
<a name="line1348"></a>  var uri = this.createDataUri(goog.userAgent.IE ? hostPrefix : null, path);
<a name="line1349"></a>  this.channelDebug_.debug(&#39;GetBackChannelUri: &#39; + uri);
<a name="line1350"></a>  return uri;
<a name="line1351"></a>};
<a name="line1352"></a>
<a name="line1353"></a>
<a name="line1354"></a>/**
<a name="line1355"></a> * Creates a data Uri applying logic for secondary hostprefix, port
<a name="line1356"></a> * overrides, and versioning.
<a name="line1357"></a> * @param {string?} hostPrefix The host prefix.
<a name="line1358"></a> * @param {string} path The path on the host.
<a name="line1359"></a> * @param {number} opt_overridePort Optional override port.
<a name="line1360"></a> * @return {goog.Uri} The data URI.
<a name="line1361"></a> */
<a name="line1362"></a>goog.net.BrowserChannel.prototype.createDataUri =
<a name="line1363"></a>    function(hostPrefix, path, opt_overridePort) {
<a name="line1364"></a>  var locationPage = window.location;
<a name="line1365"></a>  var hostName;
<a name="line1366"></a>  if (hostPrefix) {
<a name="line1367"></a>    hostName = hostPrefix + &#39;.&#39; + locationPage.hostname;
<a name="line1368"></a>  } else {
<a name="line1369"></a>    hostName = locationPage.hostname;
<a name="line1370"></a>  }
<a name="line1371"></a>
<a name="line1372"></a>  var port = opt_overridePort || locationPage.port;
<a name="line1373"></a>
<a name="line1374"></a>  var uri = goog.Uri.create(locationPage.protocol, null, hostName, port, path);
<a name="line1375"></a>  if (this.extraParams_) {
<a name="line1376"></a>    goog.structs.forEach(this.extraParams_, function(value, key, coll) {
<a name="line1377"></a>      uri.setParameterValue(key, value);
<a name="line1378"></a>    });
<a name="line1379"></a>  }
<a name="line1380"></a>
<a name="line1381"></a>  // Add the protocol version to the URI.
<a name="line1382"></a>  uri.setParameterValue(&#39;VER&#39;, goog.net.BrowserChannel.VERSION);
<a name="line1383"></a>
<a name="line1384"></a>  // Add the reconnect parameters.
<a name="line1385"></a>  this.addAdditionalParams_(uri);
<a name="line1386"></a>
<a name="line1387"></a>  return uri;
<a name="line1388"></a>};
<a name="line1389"></a>
<a name="line1390"></a>
<a name="line1391"></a>/**
<a name="line1392"></a> * Gets whether this channel is currently active. This is used to determine the
<a name="line1393"></a> * length of time to wait before retrying. This call delegates to the handler.
<a name="line1394"></a> * @return {boolean} Whether the channel is currently active.
<a name="line1395"></a> */
<a name="line1396"></a>goog.net.BrowserChannel.prototype.isActive = function() {
<a name="line1397"></a>  return this.handler_.isActive(this);
<a name="line1398"></a>};
<a name="line1399"></a>
<a name="line1400"></a>
<a name="line1401"></a>/**
<a name="line1402"></a> * Wrapper around SafeTimeout which calls the start and end execution hooks
<a name="line1403"></a> * with a try...finally block.
<a name="line1404"></a> * @param {Function} fn The callback function.
<a name="line1405"></a> * @param {number} ms The time in MS for the timer.
<a name="line1406"></a> * @return {number} The ID of the timer.
<a name="line1407"></a> */
<a name="line1408"></a>goog.net.BrowserChannel.setTimeout = function(fn, ms) {
<a name="line1409"></a>  if (!goog.isFunction(fn)) {
<a name="line1410"></a>    throw Error(&#39;Fn must not be null and must be a function&#39;);
<a name="line1411"></a>  }
<a name="line1412"></a>  return goog.global.setTimeout(function() {
<a name="line1413"></a>    goog.net.BrowserChannel.onStartExecution();
<a name="line1414"></a>    try {
<a name="line1415"></a>      fn();
<a name="line1416"></a>    } finally {
<a name="line1417"></a>      goog.net.BrowserChannel.onEndExecution();
<a name="line1418"></a>    }
<a name="line1419"></a>  }, ms);
<a name="line1420"></a>};
<a name="line1421"></a>
<a name="line1422"></a>
<a name="line1423"></a>/**
<a name="line1424"></a> * Helper function to call the start hook
<a name="line1425"></a> */
<a name="line1426"></a>goog.net.BrowserChannel.onStartExecution = function() {
<a name="line1427"></a>  goog.net.BrowserChannel.startExecutionHook_();
<a name="line1428"></a>};
<a name="line1429"></a>
<a name="line1430"></a>
<a name="line1431"></a>/**
<a name="line1432"></a> * Helper function to call the end hook
<a name="line1433"></a> */
<a name="line1434"></a>goog.net.BrowserChannel.onEndExecution = function() {
<a name="line1435"></a>  goog.net.BrowserChannel.endExecutionHook_();
<a name="line1436"></a>};
<a name="line1437"></a>
<a name="line1438"></a>
<a name="line1439"></a>/**
<a name="line1440"></a> * Returns the singleton event target for stat events.
<a name="line1441"></a> * @return {goog.events.EventTarget} The event target for stat events.
<a name="line1442"></a> */
<a name="line1443"></a>goog.net.BrowserChannel.getStatEventTarget = function() {
<a name="line1444"></a>  return goog.net.BrowserChannel.statEventTarget_;
<a name="line1445"></a>};
<a name="line1446"></a>
<a name="line1447"></a>
<a name="line1448"></a>/**
<a name="line1449"></a> * Helper function to call the stat event callback.
<a name="line1450"></a> * @param {goog.net.BrowserChannel.Stat} stat The stat.
<a name="line1451"></a> */
<a name="line1452"></a>goog.net.BrowserChannel.notifyStatEvent = function(stat) {
<a name="line1453"></a>  var target = goog.net.BrowserChannel.statEventTarget_;
<a name="line1454"></a>  target.dispatchEvent(
<a name="line1455"></a>      new goog.net.BrowserChannel.StatEvent(target, stat));
<a name="line1456"></a>};
<a name="line1457"></a>
<a name="line1458"></a>
<a name="line1459"></a>/**
<a name="line1460"></a> * A LogSaver that can be used to accumulate all the debug logs for
<a name="line1461"></a> * BrowserChannels so they can be sent to the server when a problem is
<a name="line1462"></a> * detected.
<a name="line1463"></a> */
<a name="line1464"></a>goog.net.BrowserChannel.LogSaver = {};
<a name="line1465"></a>
<a name="line1466"></a>/**
<a name="line1467"></a> * Buffer for accumulating the debug log
<a name="line1468"></a> * @type {goog.structs.CircularBuffer}
<a name="line1469"></a> * @private
<a name="line1470"></a> */
<a name="line1471"></a>goog.net.BrowserChannel.LogSaver.buffer_ =
<a name="line1472"></a>    new goog.structs.CircularBuffer(1000);
<a name="line1473"></a>
<a name="line1474"></a>/**
<a name="line1475"></a> * Whether we&#39;re currently accumulating the debug log.
<a name="line1476"></a> * @type {boolean}
<a name="line1477"></a> * @private
<a name="line1478"></a> */
<a name="line1479"></a>goog.net.BrowserChannel.LogSaver.enabled_ = false;
<a name="line1480"></a>
<a name="line1481"></a>
<a name="line1482"></a>/**
<a name="line1483"></a> * Formatter for saving logs.
<a name="line1484"></a> * @type {goog.debug.Formatter}
<a name="line1485"></a> * @private
<a name="line1486"></a> */
<a name="line1487"></a>goog.net.BrowserChannel.LogSaver.formatter_ = new goog.debug.TextFormatter();
<a name="line1488"></a>
<a name="line1489"></a>/**
<a name="line1490"></a> * Returns whether the LogSaver is enabled.
<a name="line1491"></a> * @return {boolean} Whether saving is enabled or disabled.
<a name="line1492"></a> */
<a name="line1493"></a>goog.net.BrowserChannel.LogSaver.isEnabled = function() {
<a name="line1494"></a>  return goog.net.BrowserChannel.LogSaver.enabled_
<a name="line1495"></a>};
<a name="line1496"></a>
<a name="line1497"></a>
<a name="line1498"></a>/**
<a name="line1499"></a> * Enables of disables the LogSaver.
<a name="line1500"></a> * @param {boolean} enable Whether to enable or disable saving.
<a name="line1501"></a> */
<a name="line1502"></a>goog.net.BrowserChannel.LogSaver.setEnabled = function(enable) {
<a name="line1503"></a>  if (enable == goog.net.BrowserChannel.LogSaver.enabled_) {
<a name="line1504"></a>    return;
<a name="line1505"></a>  }
<a name="line1506"></a>
<a name="line1507"></a>  var fn = goog.net.BrowserChannel.LogSaver.addLogRecord;
<a name="line1508"></a>  var logger = goog.debug.Logger.getLogger(&#39;goog.net&#39;);
<a name="line1509"></a>  if (enable) {
<a name="line1510"></a>    logger.addHandler(fn);
<a name="line1511"></a>  } else {
<a name="line1512"></a>    logger.removeHandler(fn);
<a name="line1513"></a>  }
<a name="line1514"></a>};
<a name="line1515"></a>
<a name="line1516"></a>
<a name="line1517"></a>/**
<a name="line1518"></a> * Adds a log record.
<a name="line1519"></a> * @param {goog.debug.LogRecord} logRecord the LogRecord.
<a name="line1520"></a> */
<a name="line1521"></a>goog.net.BrowserChannel.LogSaver.addLogRecord = function(logRecord) {
<a name="line1522"></a>  goog.net.BrowserChannel.LogSaver.buffer_.add(
<a name="line1523"></a>      goog.net.BrowserChannel.LogSaver.formatter_.formatRecord(logRecord));
<a name="line1524"></a>};
<a name="line1525"></a>
<a name="line1526"></a>/**
<a name="line1527"></a> * Returns the log as a single string.
<a name="line1528"></a> * @return {string} The log as a single string.
<a name="line1529"></a> */
<a name="line1530"></a>goog.net.BrowserChannel.LogSaver.getBuffer = function() {
<a name="line1531"></a>  return goog.net.BrowserChannel.LogSaver.buffer_.getValues().join(&#39;&#39;);
<a name="line1532"></a>};
<a name="line1533"></a>
<a name="line1534"></a>
<a name="line1535"></a>/**
<a name="line1536"></a> * Clears the buffer
<a name="line1537"></a> */
<a name="line1538"></a>goog.net.BrowserChannel.LogSaver.clearBuffer = function() {
<a name="line1539"></a>  goog.net.BrowserChannel.LogSaver.buffer_.clear();
<a name="line1540"></a>};
<a name="line1541"></a>
<a name="line1542"></a>
<a name="line1543"></a>
<a name="line1544"></a>/**
<a name="line1545"></a> * Interface for the browser channel handler
<a name="line1546"></a> * @constructor
<a name="line1547"></a> */
<a name="line1548"></a>goog.net.BrowserChannel.Handler = function() {
<a name="line1549"></a>};
<a name="line1550"></a>
<a name="line1551"></a>
<a name="line1552"></a>/**
<a name="line1553"></a> * Callback handler for when a batch of response arrays is received from the
<a name="line1554"></a> * server.
<a name="line1555"></a> * @type {Function?}
<a name="line1556"></a> */
<a name="line1557"></a>goog.net.BrowserChannel.Handler.prototype.channelHandleMultipleArrays = null;
<a name="line1558"></a>
<a name="line1559"></a>
<a name="line1560"></a>/**
<a name="line1561"></a> * Whether it&#39;s okay to make a request to the server. A handler can return
<a name="line1562"></a> * false if the channel should fail. For example, if the user has logged out,
<a name="line1563"></a> * the handler may want all requests to fail immediately.
<a name="line1564"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1565"></a> * @return {goog.net.BrowserChannel.Error} An error code. The code should
<a name="line1566"></a> * return goog.net.BrowserChannel.Error.OK to indicate it&#39;s okay. Any other
<a name="line1567"></a> * error code will cause a failure.
<a name="line1568"></a> */
<a name="line1569"></a>goog.net.BrowserChannel.Handler.prototype.okToMakeRequest =
<a name="line1570"></a>    function(browserChannel) {
<a name="line1571"></a>  return goog.net.BrowserChannel.Error.OK;
<a name="line1572"></a>};
<a name="line1573"></a>
<a name="line1574"></a>
<a name="line1575"></a>/**
<a name="line1576"></a> * Indicates the BrowserChannel has successfully negotiated with the server
<a name="line1577"></a> * and can now send and receive data.
<a name="line1578"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1579"></a> */
<a name="line1580"></a>goog.net.BrowserChannel.Handler.prototype.channelOpened =
<a name="line1581"></a>    function(browserChannel) {
<a name="line1582"></a>};
<a name="line1583"></a>
<a name="line1584"></a>
<a name="line1585"></a>/**
<a name="line1586"></a> * New input is available for the application to process.
<a name="line1587"></a> *
<a name="line1588"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1589"></a> * @param {Array} array The data array.
<a name="line1590"></a> */
<a name="line1591"></a>goog.net.BrowserChannel.Handler.prototype.channelHandleArray =
<a name="line1592"></a>    function(browserChannel, array) {
<a name="line1593"></a>
<a name="line1594"></a>};
<a name="line1595"></a>
<a name="line1596"></a>
<a name="line1597"></a>/**
<a name="line1598"></a> * Indicates an error occurred on the BrowserChannel.
<a name="line1599"></a> *
<a name="line1600"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1601"></a> * @param {goog.net.BrowserChannel.Error} error The error code.
<a name="line1602"></a> */
<a name="line1603"></a>goog.net.BrowserChannel.Handler.prototype.channelError =
<a name="line1604"></a>    function(browserChannel, error) {
<a name="line1605"></a>};
<a name="line1606"></a>
<a name="line1607"></a>
<a name="line1608"></a>/**
<a name="line1609"></a> * Indicates the BrowserChannel is closed.
<a name="line1610"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1611"></a> */
<a name="line1612"></a>goog.net.BrowserChannel.Handler.prototype.channelClosed =
<a name="line1613"></a>    function(browserChannel) {
<a name="line1614"></a>};
<a name="line1615"></a>
<a name="line1616"></a>
<a name="line1617"></a>/**
<a name="line1618"></a> * Gets any parameters that should be added at the time another connection is
<a name="line1619"></a> * made to the server.
<a name="line1620"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1621"></a> * @return {Object} Extra parameter keys and values to add to the
<a name="line1622"></a> *                  requests.
<a name="line1623"></a> */
<a name="line1624"></a>goog.net.BrowserChannel.Handler.prototype.getAdditionalParams =
<a name="line1625"></a>    function(browserChannel) {
<a name="line1626"></a>  return {};
<a name="line1627"></a>};
<a name="line1628"></a>
<a name="line1629"></a>
<a name="line1630"></a>/**
<a name="line1631"></a> * Gets the URI of an image that can be used to test network connectivity.
<a name="line1632"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1633"></a> * @return {goog.Uri?} A custom URI to load for the network test.
<a name="line1634"></a> */
<a name="line1635"></a>goog.net.BrowserChannel.Handler.prototype.getNetworkTestImageUri =
<a name="line1636"></a>    function(browserChannel) {
<a name="line1637"></a>  return null;
<a name="line1638"></a>};
<a name="line1639"></a>
<a name="line1640"></a>
<a name="line1641"></a>/**
<a name="line1642"></a> * Gets whether this channel is currently active. This is used to determine the
<a name="line1643"></a> * length of time to wait before retrying.
<a name="line1644"></a> * @param {goog.net.BrowserChannel} browserChannel The browser channel.
<a name="line1645"></a> * @return {boolean} Whether the channel is currently active.
<a name="line1646"></a> */
<a name="line1647"></a>goog.net.BrowserChannel.Handler.prototype.isActive = function(browserChannel) {
<a name="line1648"></a>  return true;
<a name="line1649"></a>};
</pre>


</body>
</html>
